package com.example.springsecurity.controller;

import com.example.springsecurity.arima.ARIMA;
import com.example.springsecurity.common.Result;
import com.example.springsecurity.service.HiveService;
import com.example.springsecurity.utill.DataUtills;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.text.SimpleDateFormat;
import java.util.List;

@RestController
@RequestMapping("/data")
@Api(value = "数据功能", tags = "数据功能")
public class DataController {

    @Resource
    HiveService hiveService;

    @ApiOperation("全国不同省份生猪价格发布数量分布")
    @GetMapping("/t1")
    public Result<?> t1(String category) throws SQLException {
        return Result.success(hiveService.selectFromTableExSql("t1","where category='"+category+"'"));

    }

    @ApiOperation("全国不同省市的生猪价格分布")
    @GetMapping("/t2")
    public Result<?> t2(String province,String category) throws SQLException {
        return Result.success(hiveService.selectFromTableExSql("t2","where category='"+category+"' and province='"+province+"'"));
    }

    @ApiOperation("省平均价格")
    @GetMapping("/t3")
    public Result<?> t3(String category) throws SQLException {
        return Result.success(hiveService.selectFromTableExSql("t3","where category='"+category+"'"));
    }

    @ApiOperation("省价格预测")
    @GetMapping("/t4")
    public Result<?> t4(String province,String category) throws SQLException {
        SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd");
        Date date=new Date();
        List<Double> arraylist= DataUtills.toDouble(hiveService.selectFromTableExSql("t5","where category='"+category+"' and province='"+province+"'"),4);
        double[] dataArray=new double[arraylist.size()-1];
        for(int i=0;i<arraylist.size()-1;i++)
            dataArray[i]=arraylist.get(i);

        ARIMA arima=new ARIMA(dataArray);
        int []model=arima.getARIMAmodel();
        List<String> predict=new ArrayList<>();
        predict.add("Predict");
        predict.add(province);//province
        predict.add(dateFormat.format(new Date(date.getTime() +(24 * 60 * 60 * 1000))));//Predict date
        predict.add(arima.aftDeal(arima.predictValue(model[0],model[1]))+"");//Predict value
        predict.add(Math.abs((arima.aftDeal(arima.predictValue(model[0],model[1]))-arraylist.get(arraylist.size()-1))/arraylist.get(arraylist.size()-1)*100)+"%");//Predict error
        return Result.success(predict);
    }

    @ApiOperation("省最近一个月价格走势")
    @GetMapping("/t5")
    public Result<?> t5(String province,String category) throws SQLException {
        SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date();
        StringBuilder sql=new StringBuilder("where `date` in (");
        for (int i=0;i<=6;i++){
            sql.append("'"+dateFormat.format(new Date(date.getTime() - (long)i * 5 * 24 * 60 * 60 * 1000-(24 * 60 * 60 * 1000)))+"'");
            if(i!=6){
                sql.append(",");
            }
        }
        sql.append(")");
        sql.append(" and category='"+category+"' and province='"+province+"'");
        return Result.success(hiveService.selectFromTableExSql("t5",sql.toString()));
    }

}
